//
// Created by Never on 2019/8/6.
//

#ifndef MODELFORLRP_WRITERESULTS_H
#define MODELFORLRP_WRITERESULTS_H

#include <vector>
#include <fstream>
#include <iostream>
#include "json/json.h"
#include "ParameterForRoutesRelatedProblem.h"
#include "Result.h"
#include "RunParaAndResult.h"
#include "NeighborSpaceOfGivenCus.h"

using namespace std;

class WriteResults {
private:
    static string doubleWithGivenPrecision(double number, int precision=3);
    static bool isFileExist(const string &filename);
    static void writeRootToFile(Json::Value &root, const string &filename);
    static void recordRoutesAsArray(Json::Value &root, const vector<vector<int>> &routes, const string &key);
    static void recordResult(Json::Value &root, const string& suffix, const Result *result,
                             const vector<vector<int>> &initialRoutes);
    static void analyseRoutes(Json::Value &root, const string &key, const vector<vector<int>> &routes,
                              const NeighborSpaceOfGivenCus &neighborSpace, const Instance &ins);
    static void distanceBetCusToAllDepots(Json::Value &root, const vector<vector<DisType>> &dis, int depotCount,
                                          int cusCount);

#if defined(RunModeDebug)
    static void recordIntermediateInfo(Json::Value &root, const Result* result);
    static void recordUnImprovementSteps(Json::Value &root, const vector<vector<int>> &unImproveSteps);
#endif
public:
    static bool isResultImproved(const RunParaAndResult &runParaAndResult);
    void writeResults(vector<vector<vector<int>>> depotsToCustomers, vector<int> depotsToVehicleNumber,
                      vector<int> depotsToDemands, string filename);
    void
    recordFinalResult(string &resultFileName, bool isSolved, int modelTime, int exploitTrials, int exploreTrials,
                      int totalCost, string &subTourPolicy, int lastImprovedTrail, int mode, string &modelType,
                      string &initialAlgForLKH3, int subTourNumber, int vehiclesCost, int depotsCost, int routesCost,
                      double capThres, double vehicleFactor, string improvementStrategy, int improvementTimeInLS,
                      int escapeLocalOptTime, int exploreNeighborCount, double totalTime, vector<bool> solution);
    static void recordResult(const RunParaAndResult &runParaAndResult);
};


#endif //MODELFORLRP_WRITERESULTS_H
